package net.oschina.bilbodai.common.beanutil.bean.code;

import net.oschina.bilbodai.common.beanutil.bean.core.PropertyDumpInfo;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import static org.objectweb.asm.Opcodes.INVOKESTATIC;

/**
 * @author daimao  Date: 16/5/28 Time: 下午4:34
 * @version $Id$
 */
public class StringToEnumApplier extends DumpOverrideAwareApplier {

    public StringToEnumApplier(PropertyDumpInfo dumpInfo) {
        super(dumpInfo);
    }

    @Override
    protected void loadSrcCastPropertyOntoStack(MethodVisitor mv) {
        loadSrcCastPropertyOntoStack0(mv);
    }

    @Override
    protected void doApply(MethodVisitor mv) {
        loadTargetCastOntoStack0(mv);

        loadSrcCastPropertyOntoStack(mv);
        Class<?> targetFieldType = dumpInfo.getTargetField().getType();
        mv.visitMethodInsn(INVOKESTATIC,
                Type.getInternalName(targetFieldType),
                "valueOf",
                "(Ljava/lang/String;)" + Type.getDescriptor(targetFieldType), false);
        invokeTargetPropertySetter0(mv);
    }

}